/**
 * Created by zhongwenwen on 2017/9/3.
 */
define(["jquery","template","form","zhcn","validate"],function($,template){

    //编辑页面的url中能获取到id值是location中的search:"?tc_id=34"
    if(location.search.length>0){
    //或者location.search.charAt( 0 ) == '?'来判断是否有参数
        modifyTeacher();
    }else{
        addTeacher();
    }

   //添加讲师
    function addTeacher(){

        var html = template("teacherTpl",{
            tc_nav : "添加讲师",
            tc_btn_value : "添加"
        });
        $("#addTeacher").html(html);
        //表单验证
        $("#addForm").validate({
            description:{
                tcname:{
                    //配置错误信息
                    required: "请输入用户名"
                },
                tcpass:{
                    //配置错误信息
                    required: "请输入密码",
                    pattern: "必须填写数字"
                },
                joindate: {
                    required: "请选择日期"
                }
            }
        });

        //提交数据
        $("#addTeacher").on("submit","#addForm",function(){

            //var formData = $(this).serialize();//获取表单数据
            //console.log(formData);
            //$.ajax({
            //    url:"/api/teacher/add",
            //    type: "post",
            //    data: formData,
            //    success: function(info){
            //        if(info.code==200){
            //            alert(info.msg);
            //            location.pathname = "/teacher/list"
            //        }
            //    }
            //})

            ////用form表单插件,会自动帮我们获取表单的数据
            $(this).ajaxSubmit({
                    url:"/api/teacher/add",
                    type: "post",
                    success: function(info){
                        if(info.code==200){
                            alert(info.msg);
                            location.pathname = "/teacher/list"
                        }
                    }
            })
            return false;
        })
    }

    //编辑讲师
    function modifyTeacher(){

        //获取当前数据
        var tc_id = location.search.split("=")[1]-0;
        $.ajax({
            url: "/api/teacher/edit",
            type:"get",
            data: {tc_id: tc_id},
            success: function(info){
                if(info.code==200){
                    info.result.tc_nav="讲师编辑";
                    info.result.tc_btn_value = "保存";
                    var html = template("teacherTpl",info.result);
                    $("#addTeacher").html(html);
                   //表单验证
                    $("#addForm").validate({
                        description:{
                            tcname:{
                                //配置错误信息
                                required: "请输入用户名"
                            },
                            tcpass:{
                                //配置错误信息
                                required: "请输入密码",
                                pattern: "必须填写数字"
                            },
                            joindate: {
                                required: "请选择日期"
                            }
                        }
                    });
                    ////修改以后提交
                    $("#addTeacher").on("submit","#addForm",function(){

                        //var formData = $(this).serialize();//获取表单数据
                        //$.ajax({
                        //    url:"/api/teacher/update",
                        //    type: "post",
                        //    data: formData,
                        //    success: function(info){
                        //        if(info.code==200){
                        //            alert(info.msg);
                        //            location.pathname = "/teacher/list"
                        //        }
                        //    }
                        //})

                       // 用form表单插件,会自动帮我们获取表单的数据
                        $(this).ajaxSubmit({
                            url:"/api/teacher/update",
                            type: "post",
                            success: function(info){
                                if(info.code==200){
                                    alert(info.msg);
                                    location.pathname = "/teacher/list"
                                }
                            }
                        })
                        return false;//阻止提交的默认行为
                    })
                }
            }
        })
    }
})

